package com.juzi.wopu.web;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.juzi.wopu.domain.SysCode;
import com.juzi.wopu.domain.SysProduct;
import com.juzi.wopu.domain.SysProductResponse;
import com.juzi.wopu.query.PageList;
import com.juzi.wopu.query.SysCodeQuery;
import com.juzi.wopu.query.SysProductQuery;
import com.juzi.wopu.service.IImageService;
import com.juzi.wopu.service.ISysCodeService;
import com.juzi.wopu.service.ISysProductService;
import com.juzi.wopu.service.impl.IImageServiceImpl;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class SysProductAction extends CRUDAaction<SysProduct> {
	
	private static final long serialVersionUID = 1L;

	// 注入Service接口
	private ISysProductService sysProductService;
	
	private IImageService imageService=new IImageServiceImpl();

	private SysProduct sysProduct = new SysProduct();

	private ISysCodeService sysCodeService;
	// 实例化一个查询对象
	private SysProductQuery baseQuery = new SysProductQuery();
	
	private PageList pageList;	
	
	private List<SysProductResponse> data=new ArrayList<>();	
	
	private SysCodeQuery sysCodeQuery = new SysCodeQuery();
	
	private File upload;
	private String contentType;
	private String fileName;
	
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}
	
	public String getUploadContentType() {
		return contentType;
	}

	public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getUploadFileName() {
		return fileName;
	}

	public void setUploadFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setSysProductService(ISysProductService sysProductService) {
		this.sysProductService = sysProductService;
	}

	public void setSysCodeService(ISysCodeService sysCodeService) {
		this.sysCodeService = sysCodeService;
	}

	@Override
	protected void list() {
		if(baseQuery.getiType()!=null && baseQuery.getiType()==-1){
			baseQuery.setiType(null);
		}
		if(baseQuery.getiTypeSub()!=null && baseQuery.getiTypeSub()==-1){
			baseQuery.setiTypeSub(null);
		}
		this.pageList = sysProductService.findByQuery(baseQuery);
		List<SysCode> list=sysCodeService.getAll();
		List<SysCode> typeCode=new ArrayList<>();
		List<SysCode> subTypeCode=new ArrayList<>();
		Map<Long, String> map=new HashMap<>();
		if(list!=null && list.size()>0){
			for(SysCode sysCode:list){
				map.put(sysCode.getiId(), sysCode.getsName());
				if(sysCode.getiType()!=null && sysCode.getiType()==1L
						&& sysCode.getiParentId()!=null && sysCode.getiParentId()==99L){
					typeCode.add(sysCode);
			}
				if((sysCode.getiType()!=null && sysCode.getiType()==10L) && 
						(sysCode.getiParentId()!=null &&
						(sysCode.getiParentId()==909L || sysCode.getiParentId()==908L))){
					subTypeCode.add(sysCode);
				}
			}
		}
		if(pageList!=null && pageList.getRows()!=null && pageList.getRows().size()>0){
			@SuppressWarnings("unchecked")
			List<Object> rows=pageList.getRows();
			for(Object pro:rows){
				SysProductResponse response=new SysProductResponse((SysProduct)pro);
				response.setTypeName(response.getSysCode().getsName());
				response.setSubTypeName(map.get(response.getiTypeSub()));
				String imgPath=imageService.downloadSmallImg(((SysProduct)pro).getsImgPath());
				response.setSmallImgPath(imgPath);
				data.add(response);
			}
		}
		
		putContext("typeCode", typeCode);
		putContext("subTypeCode", subTypeCode);
	}
		
	@Override
	public String input() throws Exception {
		List<SysCode> list=sysCodeService.getAll();
		List<SysCode> typeCode=new ArrayList<>();
		List<SysCode> subTypeCode=new ArrayList<>();
		if(list!=null && list.size()>0){
			for(SysCode sysCode:list){
				if(sysCode.getiType()!=null && sysCode.getiType()==1L
						&& sysCode.getiParentId()!=null && sysCode.getiParentId()==99L){
					typeCode.add(sysCode);
				}
				if((sysCode.getiType()!=null && sysCode.getiType()==10L) && 
						(sysCode.getiParentId()!=null &&
						(sysCode.getiParentId()==909L || sysCode.getiParentId()==908L))){
					subTypeCode.add(sysCode);
				}
			}
		}
		putContext("typeCode", typeCode);
		putContext("subTypeCode", subTypeCode);
		return INPUT;
	}

	public void validateSave() {
		if (StringUtils.isBlank(sysProduct.getsName())) {
			addFieldError("name", "名称不能为空");
		}
	}

	/**
	 * 处理保存和修改方法
	 */
	@Override
	@InputConfig(methodName = "input")
	public String save() throws Exception {
	
		SysCode sysCode = sysProduct.getSysCode();
		if(sysCode != null && sysCode.getiId() == -1L){
			sysProduct.setSysCode(null);
		}
		
		if (id == null) {
			if(upload!=null){
				String imgPath=imageService.upload(upload, sysProduct.getiTypeSub()+"");
				sysProduct.setsImgPath(imgPath);
			}else {
				sysProduct.setsImgPath("");
			}
			baseQuery.setCurrentPage(Integer.MAX_VALUE);
			sysProduct.setDtInsert(new Date());
		}else {
			if(upload!=null){
				String imgPath=imageService.upload(upload, sysProduct.getiTypeSub()+"");
				sysProduct.setsImgPath(imgPath);
			}
		}
		sysProductService.save(sysProduct);
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			if (id != null) {
				sysProductService.delete(id);
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			} else {
				out.print("{\"success\":false,\"msg\":\"没有删除成功\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"success\":false,\"msg\":\"异常信息:" + e.getMessage() + "\"}");
		}
		return null;
	}

	@Override
	public void prepareInput() throws Exception {
		if (id != null) {// 最主要做回显
			sysProduct = sysProductService.get(id);// 变成栈顶对象	
		}
	}

	@Override
	public void prepareSave() throws Exception {
		if (id == null) {
			// 新增后的保存
			sysProduct = new SysProduct();
		} else {
			sysProduct = sysProductService.get(id);
			sysProduct.setSysCode(null);
		}
	}

	@Override
	public SysProduct getModel() {
		return sysProduct;
	}

	public PageList getPageList() {
		return pageList;
	}

	public SysProductQuery getBaseQuery() {
		return baseQuery;
	}

	public void setBaseQuery(SysProductQuery baseQuery) {
		this.baseQuery = baseQuery;
	}

	public SysCodeQuery getSysCodeQuery() {
		return sysCodeQuery;
	}

	public void setSysCodeQuery(SysCodeQuery sysCodeQuery) {
		this.sysCodeQuery = sysCodeQuery;
	}

	public List<SysProductResponse> getData() {
		return data;
	}

	public void setData(List<SysProductResponse> data) {
		this.data = data;
	}
}
